package com.peng.leetcode.tree;

/**
 * MinDepth
 * 111. 二叉树的最小深度
 * https://leetcode.cn/problems/minimum-depth-of-binary-tree/
 * <p>
 * Created on 2022/11/29
 *
 * @author lupeng
 */
public class MinDepth {

    public static void main(String[] args) {
        TreeNode treeNode = TreeMain.array2tree(new Integer[]{2, null, 3, null, 4, null, 5, null, 6});
        System.out.println(new MinDepth().minDepth(treeNode));
    }

    public int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        int min = Integer.MAX_VALUE;
        if (root.left != null) {
            min = Math.min(minDepth(root.left), min);
        }
        if (root.right != null) {
            min = Math.min(minDepth(root.right), min);
        }
        return min + 1;
    }

}
